from object import Object

class FSM(Object):
    def __init__(self):
        Object.__init__(self)
        
        self.setState("none")
        
    def setState(self, state, args = None):
        if args == None:
            args = {}
        self.dispatch("stopState",args)
        self.setVar("state",state)
        self.dispatch("startState",args)
            
    def dispatch(self, base, args):
        if not self.hasVar("state"):
            return False
        
        state = self.getVar("state")
            
        funcName = "%s_%s" % (base,state)
        
        if hasattr(self,funcName) and callable(getattr(self,funcName)):
            getattr(self,funcName)(funcName,args)
            return True
        return False
